﻿using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Web;
using TidyBlog.Util;
using TidyBlog.Entity;
using TidyBlog.Data;
using TidyBlog.DAL;

namespace TidyBlog.BLL
{
	/// <summary>
	/// 评论业务逻辑层
	/// </summary>
	public class CommentBLL
	{
		/// <summary>
        /// 验证评论属性
        /// </summary>
        /// <param name="comment">评论</param>
		public static void Validate(Comment comment)
		{
            if (String.IsNullOrEmpty(comment.AuthorName))
            {
                throw new ArgumentException("称呼不能为空");
            }
            if (!String.IsNullOrEmpty(comment.Email) && !Validator.IsEmail(comment.Email))
            {
                throw new ArgumentException("电子邮箱格式错误");
            }
            if (String.IsNullOrEmpty(comment.Content))
            {
                throw new ArgumentException("内容不能为空");
            }

            // 检查系统评论设置
            Setup setup = SetupBLL.Read();
            switch (setup.CommentLimit)
            {
                case 0:
                    throw new Exception("评论已关闭");

                case 2:
                    comment.Visibility = 1;
                    break;
            }
            if (setup.MinCommentSize != 0 && comment.Content.Length < setup.MinCommentSize)
            {
                throw new ArgumentException("评论字数不能少于" + setup.MinCommentSize + "个字");
            }
            if (setup.MaxCommentSize != 0 && comment.Content.Length > setup.MaxCommentSize)
            {
                throw new ArgumentException("评论字数不能多于" + setup.MaxCommentSize + "个字");
            }
            if (setup.CommentInterval > 0 &&
                CommentDAL.IsExists(comment.IP, DateTime.UtcNow.Subtract(new TimeSpan(0, 0, setup.CommentInterval))))
            {
                throw new Exception("请勿频繁发表评论");
            }

            if (!ArticleBLL.IsExist(comment.ArticleId))
            {
                throw new Exception("文章不存在");
            }

            // 默认为http协议
            if (!String.IsNullOrEmpty(comment.HomePage))
            {
                comment.HomePage = Validator.fixUrl(comment.HomePage);
            }

            comment.PostTime = DateTime.UtcNow;
		}
		
		/// <summary>
		/// 创建评论
		/// </summary>
		/// <param name="comment">评论</param>
        public static void Create(Comment comment)
        {
        	Validate(comment);
        	
        	try
        	{
        		CommentDAL.Create(comment);
        	}
        	catch
        	{
#if DEBUG
				throw;
#else
				throw new Exception("创建评论记录出错");
#endif
        	}
        }

        /// <summary>
        /// 删除评论
        /// </summary>
        /// <param name="commentIds">评论编号，多个编号用逗号隔开</param>
        public static void Delete(string commentIds)
        {
            if (!Validator.IsNumbers(commentIds))
            {
                throw new Exception("评论编号错误");
            }

            try
            {
                CommentDAL.Delete(commentIds);
            }
            catch
            {
#if DEBUG
                throw;
#else
                throw new Exception("删除评论记录出错");
#endif
            }
        }

        /// <summary>
        /// 审核评论
        /// </summary>
        /// <param name="msgIds">评论编号，多个编号用逗号隔开</param>
        public static void Review(string commentIds)
        {
            if (!Validator.IsNumbers(commentIds))
            {
                throw new Exception("评论编号错误");
            }

            try
            {
                CommentDAL.Review(commentIds);
            }
            catch
            {
#if DEBUG
                throw;
#else
                throw new Exception("更新评论记录出错");
#endif
            }
        }

        /// <summary>
        /// 读取评论列表
        /// </summary>
        /// <param name="queryParams">查询参数</param>
        /// <param name="isAsc">是否按时间顺序排列</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="currentPage">当前页</param>
        /// <returns>评论列表</returns>
        public static DataPaging<Comment> List(Dictionary<string, string> queryParams, bool isAsc, ushort pageSize, int currentPage)
        {
            DataPaging<Comment> result = null;

            try
            {
                result = CommentDAL.List(queryParams, isAsc, pageSize, currentPage);
            }
            catch
            {
#if DEBUG
                throw;
#else
                throw new Exception("读取评论列表出错");
#endif
            }

            return result;
        }

        /// <summary>
        /// 获取未审核评论数
        /// </summary>
        /// <returns>未审核评论数</returns>
        public static uint GetUnreviewedCount()
        {
            try
            {
                return CommentDAL.GetUnreviewedCount();
            }
            catch
            {
#if DEBUG
                throw;
#else
                throw new Exception("统计评论记录出错");
#endif
            }
        }
    }
}